<html><head>
    <meta charset="utf-8">
    <title>角色管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link rel="stylesheet" href="/layuiAdmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/layuiAdmin/css/admin.css" media="all">
<body layadmin-themealias="default">

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">编辑角色</div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" >
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" id="name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input" value="{{$res['name']}}">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">权限</label>
                    @if ($auths)
                        @foreach ($auths as $auth)
                            <div>
                                <div class="layui-input-block">
                                    <input type="checkbox" name="auth_id[]" title="{{$auth['name']}}" value="{{$auth['id']}}" lay-filter="lockDemo"  class="all auth_id"  {{in_array($auth['id'], explode(',' , $res['auth_id'])) ? 'checked' : ''}}><div class="layui-unselect layui-form-checkbox {{in_array($auth['id'], explode(',' , $res['auth_id'])) ? 'layui-form-checked' : ''}}"><span>{{$auth['name']}}</span><i class="layui-icon layui-icon-ok"></i></div>
                                </div>
                                <div class="layui-input-block">
                                    @foreach ($auth['child'] as $item)
                                        <input type="checkbox" name="auth_id[]" class="auth_id" title="{{$item['name']}}" value="{{$item['id']}}"  lay-filter="lockDemo1"  {{in_array($item['id'], explode(',' , $res['auth_id'])) ? 'checked' : ''}}><div class="layui-unselect layui-form-checkbox {{in_array($item['id'], explode(',' , $res['auth_id'])) ? 'layui-form-checked' : ''}}"><span>{{$item['name']}}</span><i class="layui-icon layui-icon-ok"></i></div>
                                    @endforeach
                                </div>
                            </div>
                        @endforeach
                    @else
                        <div class="layui-input-block">
                            <input type="text" name="" lay-verify="" autocomplete="off" placeholder="请先去设置权限" class="layui-input" disabled>
                        </div>
                    @endif


                </div>


                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea placeholder="请输入内容" id="remark" name="remark" class="layui-textarea">{{$res['remark']}}</textarea>
                    </div>
                </div>

                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="demo1">立即提交</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="location.reload()">重置</button>
                        <button type="button" class="layui-btn layui-btn-primary" onclick="location.href='/admin/role'">返回列表</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/layuiAdmin/layui/layui.js"></script>
<script src="/layuiAdmin/tool.js"></script>

<script>
    var id = {{$id}};
    layui.use(['form'], function() {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;

        form.on('checkbox(lockDemo)', function(obj){
            let inputObj = $(this).parent().next().children('input');
            if(obj.elem.checked){
                inputObj.next().addClass('layui-form-checked');
            }else {
                inputObj.next().removeClass('layui-form-checked');
            };

        });

        form.on('checkbox(lockDemo1)', function(obj){
            let inputObj = $(this).parent().prev().children(".all");
            if(obj.elem.checked){
                inputObj.next().addClass('layui-form-checked');
            }else {
                if($(this).siblings('.layui-form-checked').length > 0){
                    inputObj.next().addClass('layui-form-checked');
                }else {
                    inputObj.next().removeClass('layui-form-checked');
                }

            };

        });

        form.on('submit(demo1)', function(data) {

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                }
            });

            var data = [];
            data['name'] = $('#name').val();
            data['auth_id'] = [];
            $(".layui-form-checked").prev().each(function () {
                data['auth_id'].push($(this).val());
            });
            data['remark'] = $('#remark').val();

            $.post('/admin/role/edit/' + id, {
                name : data['name'],
                auth_id : data['auth_id'],
                remark : data['remark']
            } , function (rJson) {

                if(rJson.status){
                    successAlert(rJson.msg);
                }else{
                    layer.msg(rJson.msg, {time: 5000, icon:5});
                }
            });
            return false;
        });


    });
</script>


</body>
</html>